package com.chejianer.push.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

//    @Autowired
//    private BackendOperService backendOperService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.antMatcher("/**")
//                .authorizeRequests()
//                .anyRequest().authenticated()
//                .and()
//                .csrf().disable()
//                .httpBasic();
        .authorizeRequests()
                .anyRequest()
                .permitAll()
                .and()
                .csrf().disable();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

          auth.inMemoryAuthentication().withUser("user").password("123456").roles("USER");
//        InMemoryUserDetailsManagerConfigurer configurer = auth.inMemoryAuthentication();
//
//        List<BackendUser> backendUsers = backendOperService.findAllBackendUser();
//
//        if (!CollectionUtils.isEmpty(backendUsers)) {
//            backendUsers.forEach(backendUser -> configurer.withUser(backendUser.getAccount()).password(backendUser.getPassword()).roles("USER"));
//        }
        // hard code user

//        configurer.withUser(UserSetting.ADMIN).password(UserSetting.ADMIN_PASSWORD).roles("USER");
//        Map<String, String[]> userSetting = UserSetting.getSetting();
//        for (Map.Entry<String, String[]> entry : userSetting.entrySet()) {
//            configurer.withUser(entry.getKey()).password(entry.getValue()[0]).roles("USER");
//        }
    }
}
